package com.lgh.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lgh.hotel.dao.mapper.RoomTypeMapper;
import com.lgh.hotel.dao.mapper.StatisticsMapper;
import com.lgh.hotel.dao.mapper.TableMapper;
import com.lgh.hotel.dao.pojo.RoomType;
import com.lgh.hotel.dao.pojo.Statistics;
import com.lgh.hotel.dao.pojo.SysUser;
import com.lgh.hotel.dao.pojo.Table;
import com.lgh.hotel.service.StatisticsService;
import com.lgh.hotel.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    StatisticsMapper statisticsMapper;
    @Autowired
    TableMapper tableMapper;
    @Autowired
    RoomTypeMapper roomTypeMapper;

    @Override
    public void add() {

        Date date = new Date();
//        date
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

        int month = localDate.getMonthValue();

        if (month == 1 || month == 2 || month == 3) {
            Statistics statistics = statisticsMapper.selectById(1);
            statistics.setNumber(statistics.getNumber() + 1);
            statisticsMapper.updateById(statistics);
        }
        if (month == 4 || month == 5 || month == 6) {
            Statistics statistics = statisticsMapper.selectById(2);
            statistics.setNumber(statistics.getNumber() + 1);
            statisticsMapper.updateById(statistics);
        }
        if (month == 7 || month == 8 || month == 9) {
            Statistics statistics = statisticsMapper.selectById(3);
            statistics.setNumber(statistics.getNumber() + 1);
            statisticsMapper.updateById(statistics);
        }
        if (month == 10 || month == 11 || month == 12) {
            Statistics statistics = statisticsMapper.selectById(4);
            statistics.setNumber(statistics.getNumber() + 1);
            statisticsMapper.updateById(statistics);
        }
    }

    @Override
    public Result findall() {

        List<Statistics> statistics = statisticsMapper.selectList(null);

        List<String> strings = new ArrayList<>();
        List<Integer> integers = new ArrayList<>();

        for (Statistics statistic : statistics) {
            strings.add(statistic.getName());
            integers.add(statistic.getNumber());
        }

        Map<String, Object> stirngObjectMap = new HashMap<>();
        stirngObjectMap.put("name", strings);
        stirngObjectMap.put("data", integers);

        return Result.succ(stirngObjectMap);

    }

    @Override
    public Result findallTable() {

        List<Table> statistics = tableMapper.selectList(null);

        List<String> strings = new ArrayList<>();
        strings.add("已入住");
        strings.add("空房");
        List<Integer> integers = new ArrayList<>();
        Integer ruzhu = 0;
        Integer noruzhu = 0;

        for (Table table : statistics) {
            if (table.getState().equals(1)) {
                ruzhu++;
            }else {
                noruzhu++;
            }
        }
        integers.add(ruzhu);
        integers.add(noruzhu);

        Map<String, Object> stirngObjectMap = new HashMap<>();
        stirngObjectMap.put("name", strings);
        stirngObjectMap.put("data", integers);

        return Result.succ(stirngObjectMap);

    }
    @Override
    public Result findallType() {

        List<Table> statistics = tableMapper.selectList(null);

        List<String> strings = new ArrayList<>();
        List<Integer> integers = new ArrayList<>();

        Map<String, Integer> types = new HashMap<>();


        for (Table table : statistics) {
          String remark =  roomTypeMapper.selectOne(new LambdaQueryWrapper<RoomType>().eq(RoomType::getId,table.getRoomType())).getRemark();

            if (types.containsKey(remark)) {
                int value = types.get(remark).intValue();
                value++;
                types.put(remark, Integer.valueOf(value));
            } else {
                types.put(remark, Integer.valueOf(1));
            }
        }

        types.entrySet().stream().forEach(e->{
            strings.add(e.getKey());
            integers.add(e.getValue());
        });


        Map<String, Object> stirngObjectMap = new HashMap<>();
        stirngObjectMap.put("name", strings);
        stirngObjectMap.put("data", integers);

        return Result.succ(stirngObjectMap);

    }
}
